import pandas as pd
import numpy as np
import matplotlib.pyplot as plt


def read_data():
    data = pd.read_csv("example/example_data.csv")
    return data


def clean_data(data):
    numeric_cols = data.select_dtypes(include=np.number).columns.tolist()
    for col in numeric_cols:
        # data[col] = data[col].astype(float)
        data[col] = data[col].fillna(value=data[col].mean())
    data.dropna()
    data.drop_duplicates()
    return data


def exploratory_analysis(data):
    rows, columns = data.shape

    if rows < 1000:
        # 小数据集（行数少于1000）查看全量数据信息和前几行
        print("数据全部内容信息：")
        data.info()
        print("数据全部内容前几行信息：")
        print(data.to_csv(sep="\t", na_rep="nan"))
    else:
        # 大数据集查看数据前几行信息和前几行
        print("数据前几行内容信息：")
        data.head().info()
        print("数据前几行内容信息：")
        print(data.head().to_csv(sep="\t", na_rep="nan"))

    # 查看数据的基本统计信息
    print("数据基本统计信息：")
    print(data.describe().to_csv(sep="\t", na_rep="nan"))

def visualization(data):
    numeric_cols = data.select_dtypes(include=np.number).columns.tolist()
    for col in numeric_cols:
        plt.figure(figsize=(8, 6))
        plt.hist(data[col], bins=30, edgecolor='black')
        plt.xlabel(col)
        plt.ylabel('Frequency_')
        plt.title(f'Histogram of {col}')
    plt.show()

if __name__ == "__main__":
    data = read_data()
    cleaned_data = clean_data(data)
    # cleaned_data.to_csv("example/example_data.csv", index=False)
    exploratory_analysis(cleaned_data)
    visualization(cleaned_data)